<?php


namespace core\utils;


use app\model\SysDictData;
use core\constants\CacheKey;

class DictUtil
{
    static function get($dict_type)
    {
        $data = ContextUtil::getOrSet($dict_type, function () use ($dict_type) {
            return self::getList($dict_type);
        });
        $data = array_column($data, 'label', 'value');
        return $data;
    }

    static function getList($dict_type)
    {
        return CacheUtil::tag(CacheKey::DICT_TAG)
            ->remember(CacheKey::DICT_GET_KEY . $dict_type, function () use ($dict_type) {
                return self::getFromDb($dict_type);
            }, 60 * 10);
    }

    private static function getFromDb($dict_type)
    {
        return SysDictData::where([
                'dict_type' => $dict_type
            ])->field([
                'id',
                'label',
                'value',
            ])->select()
                ->toArray() ?? [];
    }
}